<div id='loading' style='display:none; position: fixed; left: 50%;  top: 50%;  margin: auto;'><img src="resources/theme/images/ajax-loader.gif"></div>
<div id="order_list_screen" class="easyui-tabs" style="padding:1px;height=620px;overflow:false;">  
    <div title="Approved Orders" closable="false" style="overflow:hidden;">  
    	<div style="padding: 10px">
			<table id="approved_orders" ></table>
		</div>
    </div>  
    <div title="Quotes/Orders Waiting Approval" closable="false" style="overflow:false;padding:0px;"> 
    	<div style="padding: 10px"> 
        	<table id="quotes_orders" ></table>
        </div>
    </div>  
    <div title="Finished Orders" iconCls="icon-reload" closable="false" style="padding:0px;" onSelect="">  
        <div style="padding: 10px">
        	<table id="finished_orders" ></table>
        </div>
    </div>
    <div title="All Orders/Quotes" iconCls="icon-reload" closable="false" style="padding:0px;" onSelect="">  
        <div style="padding: 10px">
        	<table id="all_orders" ></table>
        </div>
    </div>   
</div>

<div id="approved_order_toolbar">
	<table style="width:100%;">  
        <tr>  
            <td>  
				<a href="garmentorder" class="easyui-linkbutton" iconCls="icon-add" plain="true" >New</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editOrder('#approved_orders')">Edit</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-ok" plain="true" onclick="paymentUpdate('#approved_orders', 'Pickup Order/Enter Payment')">Pickup</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-money" plain="true" onclick="paymentUpdate('#approved_orders', 'Enter Payment')">Payments</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-upload" plain="true" onclick="uploadFile('#approved_orders')">Upload File</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-print" plain="true" onclick="printOrder('#approved_orders')">Print</a>								
			</td>
			
			<td class="toolbarlabel_right" style="padding-bottom: 1px;"> <!-- tool bar label align to the right -->
				Customer Name: <input class="easyui-validatebox" type="text" name="approved_customerName" id="approved_customerName" data-options="required:false" />
				Due Date From: <input class="easyui-datebox" style="width:80px" id="approved_fromDateId" >  
		        To: <input class="easyui-datebox" style="width:80px" id="approved_toDateId">
		        <a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true"  onclick="searchOrder('approved')">Search</a>
		        <a href="#" class="easyui-linkbutton" iconCls="icon-reset" plain="true" onclick="reset('approved')">Reset</a>
		        
		    </td>

		 </tr>
	</table>	
</div>


<div id="finished_order_toolbar">
	<table style="width:100%;">  
        <tr>  
            <td>
				<a href="#" class="easyui-linkbutton" iconCls="icon-money" plain="true" onclick="paymentUpdate('#finished_orders', 'Payment History/Enter Payment')">Payments</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-upload" plain="true" onclick="uploadFile('#finished_orders')">Upload File</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-print" plain="true" onclick="printOrder('#finished_orders')">Print</a>
			</td>
			
			<td class="toolbarlabel_right" style="padding-bottom: 1px;"> <!-- tool bar label align to the right -->
				Customer Name: <input class="easyui-validatebox" type="text" name="finished_customerName" id="finished_customerName" data-options="required:false" />
				Due Date From: <input class="easyui-datebox" style="width:80px" id="finished_fromDateId" name="finished_fromDateId">  
		        To: <input class="easyui-datebox" style="width:80px" data-options="validType:'md[\'10/11/2012\']'" id="finished_toDateId" name="finished_toDateId">
		        <a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true"  onclick="searchOrder('finished')">Search</a>
		        <a href="#" class="easyui-linkbutton" iconCls="icon-reset" plain="true" onclick="reset('finished')">Reset</a>
		        
		    </td>


		 </tr>
	</table>	
</div>

<div id="quotes_toolbar">
	<table style="width:100%;">  
        <tr>  
            <td>  
            	<a href="garmentorder" class="easyui-linkbutton" iconCls="icon-add" plain="true" >New</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-ok" plain="true" onclick="paymentUpdate('#quotes_orders', 'Approve Order/Enter Down Payment')">Approve Order</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editOrder('#quotes_orders')">Edit</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-upload" plain="true" onclick="uploadFile('#quotes_orders')">Upload File</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-print" plain="true" onclick="printOrder('#quotes_orders')">Print</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-email" plain="true" onclick="email('#quotes_orders')">Email</a>
			</td>
			
			<td class="toolbarlabel_right" style="padding-bottom: 1px;"> <!-- tool bar label align to the right -->
				Customer Name: <input class="easyui-validatebox" type="text" name="quotes_customerName" id="quotes_customerName" data-options="required:false" />
				Due Date From: <input class="easyui-datebox" style="width:80px" id="quotes_fromDateId" name="quotes_fromDateId">  
		        To: <input class="easyui-datebox" style="width:80px" id="quotes_toDateId" name="quotes_toDateId">
		        <a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true"  onclick="searchOrder('quotes')">Search</a>
		        <a href="#" class="easyui-linkbutton" iconCls="icon-reset" plain="true" onclick="reset('quotes')">Reset</a>
		        
		    </td>


		 </tr>
	</table>	
</div>

<div id="allorders_toolbar">
	<table style="width:100%;">  
        <tr>  
            <td> 
            <!--  
            	<a href="garmentorder" class="easyui-linkbutton" iconCls="icon-add" plain="true" >New</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-ok" plain="true" onclick="paymentUpdate('#quotes_orders', 'Approve Order/Enter Down Payment')">Approve Order</a>
			-->
				<a href="#" class="easyui-linkbutton" iconCls="icon-print" plain="true" onclick="printOrder('#all_orders')">Print</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-upload" plain="true" onclick="uploadFile('#all_orders')">Upload File</a>

			</td>
			
			<td class="toolbarlabel_right" style="padding-bottom: 1px;"> <!-- tool bar label align to the right -->
				Order Number: <input class="easyui-validatebox" type="text" name="allorders_number" id="allorders_number" data-options="required:false" />
				Customer Name: <input class="easyui-validatebox" type="text" name="allorders_customerName" id="allorders_customerName" data-options="required:false" />
				<a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true"  onclick="searchAllOrder()">Search</a>
		        <a href="#" class="easyui-linkbutton" iconCls="icon-reset" plain="true" onclick="resetAllOrder()">Reset</a>		        
		    </td>


		 </tr>
	</table>	
</div>


<!--  order pickup/enter payment popup -->
<div id="pickupOrderEditor" class="easyui-dialog" style="width:700px;height:450px; padding:0px 20px 0px 20px" closed="true" buttons="#dlg-buttons">
	<div id="pickupFormTitle" class="ftitle"></div>
	<form id="pickupForm" name="pickupForm" method="post" novalidate>
		<input type="hidden" id="orderId" name="orderId">
		<input type="hidden" id="fmurl" >
		<input type="hidden" id="balance" name="balance" >
		<table style="width:650px;">
			<tr><td>
			<div class="fitem"><label style="width: 120px; margin-top:0px;">Customer Name:</label><span id="pickupCustomerName"></span></div>
			</td></tr>
			<tr><td>
			
			<table id="payments" ></table>
			
			</td></tr>
			<tr><td>
				<div id="addl_payment_section">
					<fieldset>
						<legend>Received and Enter Additional Payment</legend>
						<table style="width:100%; border: 0px; border-style: solid; padding: 0px;">
							<tr><td>
							<div id="isOrderFinishedSection" class="fitem"><label style="width: 120px; margin-top:0px;">Order Finished?</label>
							<span>
								<input type="radio" name="isFinished" id="isFinished_1" value="yes" style="vertical-align:bottom" class="required" data-options="required:true" > Yes	
								<input type="radio" name="isFinished" id="isFinished_2" value="no" style="vertical-align:bottom" class="required" data-options="required:true" > No
							</span>			
							</div>			
							</td></tr>
						
							<tr><td>
							<div id="isOrderApprovedSection" class="fitem"><label style="width: 120px; margin-top:0px;">Order Approved?</label>
							<span>
								<input type="radio" name="isApproved"  value="yes" style="vertical-align:bottom" class="required" data-options="required:true" > Yes	
								<input type="radio" name="isApproved"  value="no" style="vertical-align:bottom" class="required" data-options="required:true" > No
							</span>			
							</div>			
							</td></tr>
						
						
							<tr><td>
							<div class="fitem"><label style="width: 120px; margin-top:0px;">Received Payment?</label>
							<span>
								<input type="radio" name="haspayment" value="yes" style="vertical-align:bottom" class="required" data-options="required:true" onclick="enablePayment();"> Yes	
								<input type="radio" name="haspayment" value="no" style="vertical-align:bottom" class="required" data-options="required:true" onclick="cleanPayment();"> No
							</span>			
							</div>			
							</td></tr>
							<tr><td>
								<div id="paymentTypeSection" class="fitem"><label style="width: 120px; margin-top:0px;">Payment Type:</label>
								<input type="radio" name="paymentMethod" value="Cash" style="vertical-align:bottom" class="required"> Cash
								<input type="radio" name="paymentMethod" value="Check" style="vertical-align:bottom" class="required"> Check
								<input type="radio" name="paymentMethod" value="Credit" style="vertical-align:bottom" class="required"> Credit
								</div>
							</td></tr>
							
							<tr><td>
								<div class="fitem"><label style="width: 120px; margin-top:0px;">Payment Amount:</label>
								<input id="paymentAmount" name="paymentAmount" type="text" style="text-align: left; "size="10" maxlength="10" value="" onblur=""/>
								</div>
								<a href="javascript:payFullBalance();" class="easyui-linkbutton" data-options="plain:true">Full Balance</a>
							</td></tr>			
						</table>
					</fieldset>
				</div>
			</td></tr>
		</table>
		
	</form>
</div>
<div id="dlg-buttons" style="padding:10px 10px 10px 0px">
	<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="updateOrder();">Save</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#pickupOrderEditor').dialog('close')">Cancel</a>
</div>
<!--  end order pickup/enter payment popup -->

<!--  Start of upload file form  -->
<div id="emailDialog" class="easyui-dialog" title="Send Email To Customer" style="width:600px;height:550px; padding:0px 20px 0px 20px" closed="true" buttons="#fileUpload-buttons">
	<div id="emailFormTitle" class="ftitle">Email</div>
		
	
	<form id="emailForm" name="emailForm" method="post" novalidate>
		<input type="hidden" id="customerName" name="customerName">
		<input type="hidden" id="emailOrderId" name="emailOrderId">
		<input type="hidden" id="customerId" name="customerId">
		<table style="width:100%; border: 1px;">
			<tr>
				<td>
					<div class="fitem"><label style="width: 120px; margin-top:0px;">Customer Name:</label>
					<span id="emailCustomerName"></span></div>
				</td>
				<td>
				
				</td>
			</tr>
			<tr>
				<td>
					<div class="fitem"><label style="width: 120px; margin-top:5px;">Contact Name:</label>
					<input id="contact" name="contact" type="text" style="width: 336px;" class="easyui-validatebox"  required="required" onkeyup="updateEmailContactName(this.value)"/>
					</div>
				</td>
			</tr>
			<tr>
				<td>
					<div class="fitem"><label style="width: 120px; margin-top:5px;">Email Address:</label>
					<input id="email" name="email" type="text" style="width: 336px;" class="easyui-validatebox"  required="required" />
					</div>
				</td>
			</tr>
			<tr>
				<td>
					<div class="fitem"><label style="width: 120px; margin-top:5px;">Message:</label>
					<textarea id="message" name="message"  style="height:100px;width:336px; margin-top:5px;" title="Besides the following standard content, you can add
					 additional content here" class="easyui-tooltip"></textarea>
					</div>
				</td>
			</tr>
			<tr>
				<td>
					<div class="fitem"><label style="width: 120px; margin-top:10px;">Standard Message:</label>
						<div style="position: absolute; margin-left: 120px; margin-right: 110px;">
						<p> Dear <span id="emailContactName"></span>:
						<p> Thank you for giving us the opportunity to provide a quotation! Please click on the following "View" button to access the details
							of your quotation. If you have any further questions, please do not hesitate to contact me at 908-810-8888.
						<p style="text-align: center;"> <img alt="View" src="resources/theme/images/view_button.jpg">
							<br>
							<P> Best Regards,
							<br>
							<P>${userDetails.username}
							
						</div> 						
					</div>	
				</td>
			</tr>
			
		</table>

		
	</form>
</div>

<div id="fileUpload-buttons" style="padding:10px 10px 10px 0px">
	<!-- <a href="uploadFile" class="easyui-linkbutton" iconCls="icon-ok" onclick="">Upload</a> -->
	<a href="#" class="easyui-linkbutton" iconCls="icon-sendemail" onclick="sendEmail()">Send</a>
	<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#emailDialog').dialog('close')">Cancel</a>
</div>


<!--  end of upload file form -->


<script type="text/javascript">

		$(document).ready(function () {
						
		 		 setDefaultDate('approved');
				 setDefaultDate('finished');
				 setDefaultDate('quotes');
				 
				 // if Enter key is pressed, then do searchOrder('approved') function
		       	 $('#finished_customerName').keypress(function (e) {
		       		 var key = e.which;
		       		 if(key == 13) { // the enter key code
		       			searchOrder('finished');
		       		  }
		       		});
				 
		       	 $('#approved_customerName').keypress(function (e) {
		       		 var key = e.which;
		       		 if(key == 13) { // the enter key code
		       			searchOrder('approved');
		       		  }
		       		});
		       	 
		       	 $('#quotes_customerName').keypress(function (e) {
		       		 var key = e.which;
		       		 if(key == 13) { // the enter key code
		       			searchOrder('quotes');
		       		  }
		       		});
		       	 
		       	 $('#allorders_number').keypress(function (e) {
		       		 var key = e.which;
		       		 if(key == 13) { // the enter key code
		       			searchAllOrder();
		       		  }
		       		});
		       	 
		       	 $('#allorders_customerName').keypress(function (e) {
		       		 var key = e.which;
		       		 if(key == 13) { // the enter key code
		       			searchAllOrder();
		       		  }
		       		});

		       	 $('#uploadFileForm').ajaxForm({
		            beforeSubmit: function() {
		            	// alert("Thank you for your comment!"); 
		            },
		            success: function(data) {
		            	alert("success"); 						
		            }
		        });
		       	 
		       	 
		       	 
		       	 
				$('#approved_orders').datagrid({  
			    url:'getOrderList.json',
			    queryParams: {
					status: 'approved',
					customerName: $('approved_customerName').val(),
					fromDate: $('#approved_fromDateId').datebox('getValue'),
					toDate: $('#approved_toDateId').datebox('getValue')
				},
			    rownumbers: false,
			    fitColumns: true,
			    nowrap: false,
			    remoteSort: false,
			    height:610,
			    striped: true,
			    singleSelect: true,
			    toolbar: '#approved_order_toolbar',
				columns:[[
						{title:'Number',field:'orderId'},
						{title:'Order Date',field:'datePurchased',align:'left',width:60, sortable:true,
							formatter: function(value,row,index){
									return $.format.date(new Date(value), "MM/dd/yyyy");
							}	        
						}, 
				        {title:'Est. Pickup Date',field:'estPickupDate',align:'left',width:70, sortable:true,
				        	formatter: function(value,row,index){
									return $.format.date(new Date(value), "MM/dd/yyyy");
							}	        
				        }, 
				        {title:'Customer Name',field:'customerName',align:'left',width:250, sortable:true},
				        {title:'Contact Name',field:'contact',align:'left', width:100},
				        {title:'Phone',field:'tel',align:'left', width:80},
				        // {title:'Fax',field:'fax',align:'left', width:60},
				        {title:'Email',field:'email',align:'left', width:100},
				        {title:'Address',field:'fullAddress',align:'left', width:200, hidden:true},				        
				        {title:'Price',field:'totalPrice',
				        	formatter: function(value,row,index){
								return formatCurrency(value);
							}		
				        },
				        {title:'Down Payment',field:'totalPayment',
				        	formatter: function(value,row,index){
								return formatCurrency(value);
							}		
				        },
				        {title:'Balance',field:'balance',
				        	formatter: function(value,row,index){
								return formatCurrency(value);
							}	
				        }
				        // {title:'State',field:'state',align:'left', width:30},
				        // {title:'Zip Code',field:'zip',align:'left', width:50},
				    ]],
			     onDblClickRow: function(rowIndex, rowData){
			    	window.location = 'editgarmentorder?orderId='+rowData.orderId
				 }			    
			});
				
				
				$('#quotes_orders').datagrid({  
				    url:'getOrderList.json',
				    queryParams: {
						status: 'quotes',
						customerName: $('quotes_customerName').val(),
						fromDate: $('#quotes_fromDateId').datebox('getValue'),
						toDate: $('#quotes_toDateId').datebox('getValue')
					},
				    rownumbers: false,
				    fitColumns: true,
				    nowrap: false,
				    remoteSort: false,
				    height:610,
				    striped: true,
				    singleSelect: true,
				    toolbar: '#quotes_toolbar',
					columns:[[
								{title:'Number',field:'orderId',width:40},
								{title:'Order Date',field:'datePurchased',align:'left',width:60, sortable:true,
									formatter: function(value,row,index){
											return $.format.date(new Date(value), "MM/dd/yyyy");
									}	        
								}, 
						        {title:'Est. Pickup Date',field:'estPickupDate',align:'left',width:60, sortable:true,
						        	formatter: function(value,row,index){
											return $.format.date(new Date(value), "MM/dd/yyyy");
									}	        
						        }, 
						        {title:'Customer Name',field:'customerName',align:'left',width:250},
						        {title:'Contact Name',field:'contact',align:'left', width:60},
						        {title:'Phone',field:'tel',align:'left', width:60},
						        {title:'Email',field:'email',align:'left', width:100},
						        {title:'Address',field:'fullAddress',align:'left', width:200, hidden: true},
						        {title:'Price',field:'totalPrice', width:60,
						        	formatter: function(value,row,index){
										return formatCurrency(value);
									}		
						        }
					    ]],
				     onDblClickRow: function(rowIndex, rowData){
				    	window.location = 'editgarmentorder'+'?orderId='+rowData.orderId;
					}			    
				});
						
				
				$('#finished_orders').datagrid({  
				    url:'getOrderList.json',
				    queryParams: {
						status: 'finished',
						customerName: $('finished_customerName').val(),
						fromDate: $('#finished_fromDateId').datebox('getValue'),
						toDate: $('#finished_toDateId').datebox('getValue')
					},
				    rownumbers: false,
				    fitColumns: true,
				    nowrap: false,
				    remoteSort: false,
				    height:610,
				    striped: true,
				    singleSelect: true,
				    toolbar: '#finished_order_toolbar',
					columns:[[
								{title:'Number',field:'orderId',width:40},
								{title:'Order Date',field:'datePurchased',align:'left',width:60, sortable:true,
									formatter: function(value,row,index){
											return $.format.date(new Date(value), "MM/dd/yyyy");
									}	        
								}, 
						        {title:'Est. Pickup Date',field:'estPickupDate',align:'left',width:60, sortable:true,
						        	formatter: function(value,row,index){
											return $.format.date(new Date(value), "MM/dd/yyyy");
									}	        
						        }, 
						        {title:'Customer Name',field:'customerName',align:'left',width:250},
						        {title:'Contact Name',field:'contact',align:'left', width:60},
						        {title:'Phone',field:'tel',align:'left', width:60},
						        {title:'Email',field:'email',align:'left', width:100},
						        {title:'Address',field:'fullAddress',align:'left', width:200},
						        {title:'Price',field:'totalPrice',
						        	formatter: function(value,row,index){
										return formatCurrency(value);
									}		
						        },
						        {title:'Payment',field:'totalPayment',
						        	formatter: function(value,row,index){
										return formatCurrency(value);
									}		
						        },
						        {title:'Balance',field:'balance',
						        	formatter: function(value,row,index){
										return formatCurrency(value);
									}	
						        }
					    ]],
					    onDblClickRow: function(rowIndex, rowData){
					    	window.location = 'editgarmentorder'+'?orderId='+rowData.orderId;
						}							    
				});
		});
		
		
		$('#all_orders').datagrid({  
		    url:'getAllOrderList.json',
 		    queryParams: {
 				orderId: $('allorders_number').val(),
 		    	customerName: $('allorders_customerName').val() 				
 			},
		    rownumbers: false,
		    fitColumns: true,
		    nowrap: false,
		    remoteSort: false,
		    height:610,
		    striped: true,
		    singleSelect: true,
		    toolbar: '#allorders_toolbar',
			columns:[[
					{title:'Number',field:'orderId',width:40},
					{title:'Order Date',field:'datePurchased',align:'left',width:60, sortable:true,
						formatter: function(value,row,index){
								return $.format.date(new Date(value), "MM/dd/yyyy");
						}	        
					}, 
			        {title:'Est. Pickup Date',field:'estPickupDate',align:'left',width:70, sortable:true,
			        	formatter: function(value,row,index){
								return $.format.date(new Date(value), "MM/dd/yyyy");
						}	        
			        }, 
			        {title:'Customer Name',field:'customerName',align:'left',width:250, sortable:true},
			        {title:'Contact Name',field:'contact',align:'left', width:100},
			        {title:'Phone',field:'tel',align:'left', width:80},
			        // {title:'Fax',field:'fax',align:'left', width:60},
			        {title:'Email',field:'email',align:'left', width:100},
			        {title:'Address',field:'fullAddress',align:'left', width:200, hidden:true},				        
			        {title:'Price',field:'totalPrice', width:60,
			        	formatter: function(value,row,index){
							return formatCurrency(value);
						}		
			        },
			        {title:'Payment',field:'totalPayment', width:60,
			        	formatter: function(value,row,index){
							return formatCurrency(value);
						}		
			        },
			        {title:'Balance',field:'balance', width:60,
			        	formatter: function(value,row,index){
							return formatCurrency(value);
						}	
			        }
			        // {title:'State',field:'state',align:'left', width:30},
			        // {title:'Zip Code',field:'zip',align:'left', width:50},
			    ]],
		     onDblClickRow: function(rowIndex, rowData){
		    	window.location = 'editgarmentorder?orderId='+rowData.orderId;
			 }
		});
		
		
		
		function editOrder(dataGridName){
			var row = $(dataGridName).datagrid('getSelected');			
			// alert(row.orderId);
			if (row){
				window.location = 'editgarmentorder'+'?orderId='+row.orderId;
			}else{
				$.messager.alert('Alert','Please select the Order to be edited.');
		    	return false;
			}
		}
		
		function searchOrder(orderStatus){
			 // validate Due Date From format		
			if($('#'+orderStatus+'_fromDateId').datebox('getValue')!="" && !ValidateCustomDate($('#'+orderStatus+'_fromDateId').datebox('getValue'))){
				$.messager.show({  
                    title: 'Error',
                    width: 400,
                    height: 200,
                    timeout: 5000,
                    msg: '<B>Due Date From</b> format must be dd/mm/yyyy or d/m/yyyy.',
                    style:{
                        right:'',
                        top:document.body.scrollTop+document.documentElement.scrollTop,
                        bottom:''
                    }
                });
				$('#'+orderStatus+'_fromDateId').focus();
			}
			// validate Due Date From format
			if($('#'+orderStatus+'_toDateId').datebox('getValue')!="" && !ValidateCustomDate($('#'+orderStatus+'_toDateId').datebox('getValue'))){
				$.messager.show({  
                    title: 'Error',
                    width: 400,
                    height: 200,
                    timeout: 5000,
                    msg: '<B>Due Date To</b> format must be dd/mm/yyyy or d/m/yyyy.',
                    style:{
                        right:'',
                        top:document.body.scrollTop+document.documentElement.scrollTop,
                        bottom:''
                    }
                });
				$('#'+orderStatus+'_toDateId').focus();
			}
			// rreload the datagrid using following parameters
			$('#'+orderStatus+'_orders').datagrid('load',{ 
				status: orderStatus,
				customerName: $('#'+orderStatus+'_customerName').val(),
				fromDate: $('#'+orderStatus+'_fromDateId').datebox('getValue'),
				toDate: $('#'+orderStatus+'_toDateId').datebox('getValue')
		    });
		} 
		
		function reset(orderStatus){
			setDefaultDate(orderStatus);
		
			$('#'+orderStatus+'_orders').datagrid('load',{
				status: orderStatus,
				customerName: '',
				fromDate: $('#'+orderStatus+'_fromDateId').datebox('getValue'),
				toDate: $('#'+orderStatus+'_toDateId').datebox('getValue')				
		    });
			
		}
		
		function searchAllOrder(){
			if($('#allorders_number').val()=="" && $('#allorders_customerName').val()==""){
				$.messager.show({  
                    title: 'Error',
                    width: 400,
                    height: 200,
                    timeout: 5000,
                    msg: 'Please enter Order Number or Customer Name to search.',
                    style:{
                        right:'',
                        top:document.body.scrollTop+document.documentElement.scrollTop,
                        bottom:''
                    }
                });
				$('#allorders_number').focus();
			}else {
				$('#all_orders').datagrid('load',{ 
					orderId: $('#allorders_number').val(),
	 		    	customerName: $('#allorders_customerName').val()
			    });
				
			}
			
		}
		
		function resetAllOrder(){
			$('#allorders_number').val('');
			$('#allorders_customerName').val('');
			
			$('#all_orders').datagrid('load',{ 
				orderId: $('#allorders_number').val(),
 		    	customerName: $('#allorders_customerName').val()
		    });
		}
		
		function setDefaultDate(orderStatus){
			$('#'+orderStatus+'_customerName').val('');
			var fromDate = new Date();
			fromDate.setDate(fromDate.getDate() - 15);			
			var fromDateString = (fromDate.getMonth()+1.0)+'/'+fromDate.getDate()+'/'+fromDate.getFullYear();		
			
			var toDate = new Date();
			toDate.setDate(toDate.getDate() + 30);	
			var toDateString = toDate.getMonth()+1.0+'/'+toDate.getDate()+'/'+toDate.getFullYear();
			
			if (orderStatus == "finished"){				
				$('#finished_fromDateId').datebox('setValue',fromDateString);
				$('#finished_toDateId').datebox('setValue',toDateString);
			}else if (orderStatus == "approved"){
	         	$('#approved_fromDateId').datebox('setValue',fromDateString);
				$('#approved_toDateId').datebox('setValue',toDateString);
			}else if (orderStatus == "quotes"){
				$('#quotes_fromDateId').datebox('setValue',fromDateString);
				$('#quotes_toDateId').datebox('setValue',toDateString);
			}
		}
		
		
		function finishOrder(dataGridName){		
			
		    var row = $(dataGridName).datagrid('getSelected');

		    if (row){  
		        $.messager.confirm('Confirm','<b>Are you sure you have finished following Order?</b><br><br>' + row.customerName, function(r){  
		            if (r){  
		                $.post('finishOrder.json',{orderId:row.orderId},function(result){  
		                    if (result.success){  
				                $(dataGridName).datagrid('reload');
				                $.messager.show({  
				                    title: 'Result',  
				                    msg: 'Order has been updated successfully.',
			                    	style:{
			                            right:'',
			                            top:document.body.scrollTop+document.documentElement.scrollTop,
			                            bottom:''
			                        }
				                }); 
		                    } else {		                    	
		                        $.messager.alert({   // show error message  
		                            title: 'Error',		                            
		                            width: 400,
		                            height: 200,
		                            html: result.msg
		                            // msg: result.msg  
		                        });  
		                    }  
		                },'json');  
		            }  
		        });  
		    }else{
		    	$.messager.alert('Alert','Please select the Order you have finished.');
		    	return false;
		    } 
		} 
		
		function paymentUpdate(dataGridName, dialogTitle){
			
			var row = $(dataGridName).datagrid('getSelected');
			
			if (row){
				$('#pickupForm').form('clear');
				
				titleString=dialogTitle.split('/');
				$('#pickupOrderEditor').dialog('open').dialog('setTitle',titleString[0]);
				
				$('#orderId').val(row.orderId);
				$('#pickupCustomerName').html(row.customerName);
				// $('#totalPrice').html(formatCurrency(row.totalPrice));
				// $('#totalPayment').html(formatCurrency(row.totalPayment));
				// $('#balance').html(formatCurrency(row.balance));				
				$('#balance').val(row.balance);
				$('#fmurl').val('pickupOrder.json');
				$('#pickupFormTitle').html(dialogTitle);
				
				if(titleString[0] == "Pickup Order" || titleString[0] == "Payment History"){
					$('input:radio[name=isFinished][value="yes"]').prop('checked', true);
					$('#isOrderFinishedSection').show();
					$('#isOrderApprovedSection').hide();
				}else if(titleString[0] == "Enter Payment"){
					$('input:radio[name="isFinished"][value="no"]').attr('checked',true);
					$('#isOrderFinishedSection').show();
					$('#isOrderApprovedSection').hide();
				}else if(titleString[0] == "Approve Order"){
					$('input:radio[name="isFinished"][value="no"]').attr('checked',true);
					$('input:radio[name="isApproved"][value="yes"]').attr('checked',true);
					$('#isOrderFinishedSection').hide();
					$('#isOrderApprovedSection').show();
				}
								
				
				if(row.balance==0){
					$('#addl_payment_section').hide();
				}else {
					$('#addl_payment_section').show();
				}
				
				
				$('#payments').datagrid('load',{
					orderId: row.orderId					
				});
				
				
				// url = 'pickupOrder.json';
			}else{
				$.messager.alert('Alert','Please select the Order you want to work with.');
		    	return false;
			}
		}
		
		function payFullBalance(){
			// var total = Number($('#balance').html().replace(/[^0-9\.]+/g,""));
			// alert(total);
			$('#paymentAmount').val(formatCurrency($('#balance').val()));
		}
		
		function cleanPayment(){
			$('#paymentAmount').val('');
			$('#paymentAmount').attr("disabled", true);
			$('input:radio[name="paymentMethod"]').attr('checked',false);
			$('input:radio[name="paymentMethod"]').attr('disabled', true);	
			
		}
		
		function enablePayment(){
			$('#paymentAmount').attr("disabled", false);
			$('input:radio[name="paymentMethod"]').attr('disabled', false);	
			
		}
		
		function updateOrder(){  
		    $('#pickupForm').form('submit',{  
		        url: $('#fmurl').val(),
		        onSubmit: function(){  
		            return $(this).form('validate');  		            
		        },  
		        success: function(result){
		        	var result = eval('('+result+')');  
		            if (result.success){  
		                $('#pickupOrderEditor').dialog('close');      // close the dialog  
		                $.messager.show({  
		                    title: 'Result',
		                    width: 400,
                            height: 200,
                            timeout: 5000,
		                    msg: 'Order has been updated successfully.',
		                    style:{
		                        right:'',
		                        top:document.body.scrollTop+document.documentElement.scrollTop,
		                        bottom:''
		                    }
		                });  
		                
		                $('#approved_orders').datagrid('reload');
		                $('#quotes_orders').datagrid('reload'); 
		                $('#finish_orders').datagrid('reload'); 
		            } else {
		            	// $('#dueDateMsg').html(result.dueDateMsg);
		            	/* $('#priorityMsg').html(result.priorityMsg);
		            	$('#ownerMsg').html(result.ownerMsg);
		            	$('#descMsg').html(result.descMsg); */  
		                
		            	$.messager.show({  
		                    title: 'Error',
		                    width: 400,
                            height: 200,
                            timeout: 7000,
		                    msg: result.msg,
		                    style:{
		                        right:'',
		                        top:document.body.scrollTop+document.documentElement.scrollTop,
		                        bottom:''
		                    }
		                }); 
		            }  
		        }  
		    });  
		}  
		
		
		function showPayments(dataGridName){
			
			var row = $(dataGridName).datagrid('getSelected');
			
			if (row){  
				$('#pickupOrderEditor').dialog('open').dialog('setTitle','Payments');
				
			/*	$('#payments').datagrid('load',{
					orderId: row.orderId					
				});
				*/
				
				// $('#pickupForm').form('clear');
				
// 				$('#orderId').val(row.orderId);
// 				$('#pickupCustomerName').html(row.customerName);
// 				$('#totalPrice').html(formatCurrency(row.totalPrice));
// 				$('#totalPayment').html(formatCurrency(row.totalPayment));
// 				$('#balance').html(formatCurrency(row.balance));				
// 				$('#fmurl').val('pickupOrder.json');
				
			}else{
				$.messager.alert('Alert','Please select the Order.');
		    	return false;
			}
		}
		
		
		$('#payments').datagrid({  
		    url:'getPaymentsByOrderId.json',
		    queryParams: {
				orderId: ''					
			},
		    rownumbers: true,
		    fitColumns: true,
		    nowrap: false,
		    remoteSort: false,
		    striped: true,
		    singleSelect: true,
		    showFooter: true,
			columns:[[
						{title:'Payment Date',field:'paymentDate',align:'left',width:60, sortable:true, order:'asc',
							formatter: function(value,row,index){
									return value==null?"":$.format.date(new Date(value), "MMM-dd-yyyy");
							}	        
						}, 
				        {title:'Payment Method',field:'paymentMethod',align:'left',width:60, sortable:true,   
				        }, 
				        {title:'Payment Type',field:'paymentType',align:'left', width:60},
				        {title:'Payment Amount',field:'paymentAmount',align:'left',width:60,				        	
		        			formatter: function(value,row,index){
								return formatCurrency(value);
						}
				        }
				       
				    ]]
		    
		});
		
		function uploadFile(dataGridName){
			var row = $(dataGridName).datagrid('getSelected');
						
			if (row){
				
				window.location.href = "fileHome?customerId="+row.customerId+"&orderId="+row.orderId;
				
				// $('#uploadFileForm').form('clear');
				// $('#ordersId').val(row.orderId);
				// $('#customerName').val(row.customerName);
				// $('#customerId').val(row.customerId);
				
				// alert(row.customerId);
				// $('#uploadFileDialog').dialog('open').dialog('setTitle',"Upload File");

			}else{
				$.messager.alert('Alert','Please select the Order you want to upload file to.');
		    	return false;
			}
			
		}
		
		function startUpLoad(){
			$( "#uploadFileForm" ).submit();
		
		}
		
function email(dataGridName){
			$('#emailForm').form('clear');
			var row = $(dataGridName).datagrid('getSelected');
			
			if (row){
				
				$('#emailCustomerName').html(row.customerName);
				$('#emailContactName').html(row.contact);
				$('#emailOrderId').val(row.orderId);
				// $('#emailContactName').val(row.contact);
				$('#emailForm').form('load',row);
				

				$('#emailDialog').dialog('open');
				
				// url = 'pickupOrder.json';
			}else{
				$.messager.alert('Alert','Please select the Order you want to send email.');
		    	return false;
			}
		}
		
		
		
		function printOrder(dataGridName){
			//alert('printing order');
			var row = $(dataGridName).datagrid('getSelected');
			
			if (row){
				window.open('printorder?orderID='+row.orderId,'','scrollbars=no,menubar=no,height=800,width=1000,resizable=yes,toolbar=no,location=no,status=no');
			}else{
				$.messager.alert('Alert','Please select the Order you want to work with.');
		    	return false;
			}
		}
	
		
		function updateEmailContactName(contact){			
			$('#emailContactName').html(contact);
		}
		
		
		function sendEmail(){
			// alert("sending email");
			$('#emailDialog').dialog('close');      // close the dialog
			$('#loading').show();
		    $('#emailForm').form('submit',{  
		        url: 'emailQuote',
		        onSubmit: function(){  
		            return $(this).form('validate');  		            
		        },  
		        success: function(result){
		        	// alert(result);
		        	$('#loading').hide();
		            var result = eval('('+result+')');  
		            // alert(result);
		            if (result.success){  
		                $.messager.show({  
		                    title: 'Result',  
		                    msg: 'Email has been sent successfully.',
		                    width: 400,
	                        height: 100,
		                    style:{
		                        right:'',
		                        top:document.body.scrollTop+document.documentElement.scrollTop,
		                        bottom:''
		                    }
		                }); 
		            } else {
		            	// $('#dueDateMsg').html(result.dueDateMsg);
		            	/* $('#priorityMsg').html(result.priorityMsg);
		            	$('#ownerMsg').html(result.ownerMsg);
		            	$('#descMsg').html(result.descMsg); */  
		                
		            	$.messager.show({  
		                    title: 'Error',
		                    width: 400,
                            height: 200,
                            timeout: 15000,
		                    msg: result.msg,
		                    style:{
		                        right:'',
		                        top:document.body.scrollTop+document.documentElement.scrollTop,
		                        bottom:''
		                    }
		                }); 
		            }  
		        }  
		    });  
/* 		    var row = $('#orderItemGrid').datagrid('getSelected');
		    var rowIndex = $("#orderItemGrid").datagrid("getRowIndex", row);
		    
		    if (row){  
		    			// alert('in removeToDo. RowIndex is:' + rowIndex);
		               $.post('removeOrderItem.json?lineNo='+rowIndex,{styleNo:row.styleNo, desc:row.desc, artDesc:row.artDesc, color:row.color, size:row.size, qty:row.qty, unitPrice:row.unitPrice, subTotal:row.subTotal},function(result){  
		                    if (result.success){  
				                //reload the dataGrid
		                    	$(dataGridName).datagrid('reload');

				                $.messager.show({
				                	title: 'Result',
				                	msg: 'Item has been removed successfully.',
				                    style:{
				                        right:'',
				                        top:document.body.scrollTop+document.documentElement.scrollTop,
				                        bottom:''
				                    }
				                }); 
		                    } else {		                    	
		                        $.messager.alert({   // show error message  
		                            title: 'Error',		                            
		                            width: 400,
		                            height: 200,
		                            html: result.msg
		                            // msg: result.msg  
		                        });  
		                    }  
		                },'json');		          
		    }else{
		    	$.messager.alert('Alert','Please select the item to be removed.');
		    	return false;
		    }  */
			
		}
		
		
		
/*		$(function () {
		    $('#fileupload').fileupload({
		        dataType: 'json',
		 
		        done: function (e, data) {
		            $("tr:has(td)").remove();
		            $.each(data.result, function (index, file) {
		 
		                $("#uploaded-files").append(
		                        $('<tr/>')
		                        .append($('<td/>').text(file.fileName))
		                        .append($('<td/>').text(file.fileSize))
		                        .append($('<td/>').text(file.fileType))
		                        .append($('<td/>').html("<a href='rest/controller/get/"+index+"'>Click</a>"))
		                        )//end $("#uploaded-files").append()
		            }); 
		        },
		 
		        progressall: function (e, data) {
		            var progress = parseInt(data.loaded / data.total * 100, 10);
		            $('#progress .bar').css(
		                'width',
		                progress + '%'
		            );
		        },
		 
		        dropZone: $('#dropzone')
		    });
		});
		
		
 		function startUpLoad(){
		    $('#uploadFileForm').form('submit',{  
		    	url: 'uploadFile.json',
		        onSubmit: function(){  
		            return $(this).form('validate');  		            
		        },  
		        success: function(result){
		        	// alert(result);
		            var result = eval('('+result+')');  

		            if (result.success){  
		                $('#uploadFileDialog').dialog('close');      // close the dialog  
		                // $('#customer_list').datagrid('reload');
		                $.messager.show({  
		                    title: 'Result',  
		                    msg: 'File has been uplaoded successfully.',
		                    width: 400,
	                        height: 100,
		                    style:{
		                        right:'',
		                        top:document.body.scrollTop+document.documentElement.scrollTop,
		                        bottom:''
		                    }
		                });  
		            } else {
		            	$.messager.show({  
		                    title: 'Error',
		                    width: 400,
	                        height: 200,
	                        timeout: 15000,
		                    msg: result.msg,
		                    style:{
		                        right:'',
		                        top:document.body.scrollTop+document.documentElement.scrollTop,
		                        bottom:''
		                    }
		                }); 
		            }  
		        }  
		    });
		} */
		
</script>